﻿@page "/"
@using Moments.Model
@using Moments.Service
@inject IJSRuntime JsRuntime
@inject ArticleService ArticleService
@inject ConfigService ConfigService
@inject FriendService FriendService
@inject IConfiguration Configuration
<div id="main">
    <div class="main-content">
        <div class="header">
            <div class="header-top">
                <span class="header-left">
                    <span @onclick='() => { JsRuntime.InvokeVoidAsync("scrollToTop"); }'>@ConfigService.Get("Title")</span>
                </span>
                <span class="header-right" @onclick="() => _info = true">
                    <div style="color: white;margin-right: 10px">
                        <span class="ma-1">
                            <i class="fas fa-user-friends"></i>朋友：@_friends.Count
                        </span>
                        <span class="ma-1">
                            <i class="fas fa-palette"></i>
                            文章：@_countArticle
                        </span>
                    </div>
                </span>
            </div>
            <MImage src="@ConfigService.Get("Banner")" alt="" Class="rounded-b-lg"/>
            <div class="profile">
                <div id="name">@ConfigService.Get("Name")</div>
                <img src="@ConfigService.Get("Avatar")" alt="头像">
            </div>
            <div class="says">@ConfigService.Get("Sentence")</div>
        </div>
        <div id="pjax-content">
            <div class="main-container">
                @foreach (var item in _nodeList)
                {
                    <div class="item">
                        <div class="left">
                            <img @onclick="() => {Task.FromResult(Select(item.FriendId)); _friendInfo = true;}" src="@GetAvatar(item.FriendId)" alt="头像">
                        </div>
                        <div class="right">
                            <div class="name">
                                <span style="color: #576b95" @onclick="() => {Task.FromResult(Select(item.FriendId)); _friendInfo = true;}">@GetName(item.FriendId)</span>
                            </div>
                            <div class="content">
                                <b>@item.Title</b>
                                @(new MarkupString(Utils.Substring(item.Description, int.Parse(ConfigService.Get("Maximum"))) ?? "暂无阅览，请查看原文"))
                            </div>
                            <div>
                                <div class="time">@item.PubDate.ToString("MM月dd日 tt hh:mm")</div>
                                <a href="@item.Link" target="_blank" class="more">查看原文</a>
                            </div>
                        </div>
                    </div>
                }
            </div>
        </div>
        <div style="text-align: center;padding: 10px">
            <MButton
                Outlined
                OnClick="GetMore"
                Elevation="2">
                <MIcon>fa:fas fa-solid fa-infinity</MIcon>
                加载更多
            </MButton>
        </div>
    </div>
</div>
<PModal @bind-Value="_info"
        Title="快捷面板"
        Width="500"
        OnCancel="() => _info = false">
    <MButton Depressed Link Href="/Admin" Color="primary">
        进入后台
    </MButton>

    <MDivider Height="10" Style="margin: 20px 0"></MDivider>
    <span style="float: left">
        <MIcon Style="margin-top: -4px;">mdi:mdi-clover</MIcon>
        Version @Configuration["Version"]
    </span>
    <a style="float: right" href="https://github.com/Drizzle365/Moments" target="_blank">
        <MIcon Style="margin-top: -4px;">mdi:mdi-github</MIcon>
        开源地址
    </a>
</PModal>
<PModal @bind-Value="_friendInfo"
        Title="最近动态"
        Width="500"
        OnCancel="() => _friendInfo = false">
    <div style="text-align: center">
        <MAvatar Size="84">
            <MImage Src="@_select?.Avatar" Alt="Avatar"></MImage>
        </MAvatar>
        <div>
            <MChip Class="ma-2"
                   Color="primary">
                @_select?.Name
            </MChip>
        </div>
    </div>
    <MList Subheader
           TwoLine Elevation="0">
        @foreach (var item in _selectArticles)
        {
            <MListItem Href="@item.Link" Target="_blank" Link>
                <MListItemContent>
                    <MListItemTitle>@item.Title</MListItemTitle>
                    <MListItemSubtitle>@item.Description</MListItemSubtitle>
                </MListItemContent>
                <MListItemAction>
                    <MChip Small>
                        @item.PubDate.ToString("MM月dd日")
                    </MChip>
                </MListItemAction>
            </MListItem>
        }
    </MList>
</PModal>

@code{
    long _countArticle;
    bool _info;
    bool _friendInfo;
    List<Article> _nodeList = new();
    List<Friend> _friends = new();
    int _p = 1;
    Friend? _select;
    List<Article> _selectArticles = new();

    protected override async Task OnInitializedAsync()
    {
        _countArticle = await ArticleService.CountAsync();
        _nodeList = await ArticleService.ListAsync();
        _friends = await FriendService.ListAsync();
    }

    string GetName(int? id)
    {
        return _friends.Find(x => x.FriendId == id)?.Name ?? "未命名";
    }

    string GetAvatar(int? id)
    {
        return _friends.Find(x => x.FriendId == id)?.Avatar ?? "https://gravatar.loli.net/avatar/c243c9fe3d177bc3f656ff4554e4cbb0?s=100&d=mm&r=g";
    }

    async Task GetMore()
    {
        _p++;
        var more = await ArticleService.ListAsync(page: _p);
        Console.WriteLine(more.Count);
        _nodeList.AddRange(more);
    }

    async Task Select(int target)
    {
        var f = _friends.Find(x => x.FriendId == target);
        _select = f;
        _selectArticles = await ArticleService.ListAsync(target);
    }

}